# Contributor: omni <omni+alpine@hack.org>
# Maintainer: omni <omni+alpine@hack.org>
pkgname=py3-psycopg
pkgver=3.1.18
pkgrel=1
pkgdesc="PostgreSQL adapter for python"
options="!check" # Requires running database
url="https://www.psycopg.org/"
arch="all"
license="LGPL-2.0-or-later"
makedepends="
	cython
	libpq-dev
	py3-gpep517
	py3-setuptools
	py3-tomli
	py3-wheel
	python3-dev
	"
subpackages="
	$pkgname-c-pyc:_c_pyc
	$pkgname-pyc
	$pkgname-c:_c
	"
source="https://github.com/psycopg/psycopg/archive/$pkgver/psycopg-$pkgver.tar.gz
	fix-missing-type-imports.patch
	typing-ext.patch
	psycopg-binary.patch
	"
builddir="$srcdir/psycopg-$pkgver"

prepare() {
	default_prepare

	# Check if we need to update typing-ext.patch
	if grep -r typing_extensions $builddir; then return 1; fi
}

build() {
	cd psycopg
	gpep517 build-wheel \
		--wheel-dir .dist \
		--output-fd 3 3>&1 >&2

	cd ../psycopg_c
	gpep517 build-wheel \
		--wheel-dir .dist \
		--output-fd 3 3>&1 >&2
}

check() {
	make -C psycopg check
	make -C psycopg_c check
}

package() {
	cd psycopg
	python3 -m installer -d "$pkgdir" .dist/*.whl

	cd ../psycopg_c
	python3 -m installer -d "$pkgdir" .dist/*.whl
}

_c() {
	pkgdesc="$pkgdesc - C optimisation distribution"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove usr/lib/python*/site-packages/psycopg_c*
}

_c_pyc() {
	pkgdesc="Precompiled Python bytecode for ${subpkgname%-pyc}"
	install_if="${subpkgname%-pyc}=$pkgver-r$pkgrel pyc"

	cd "$pkgdir"
	# shellcheck disable=SC3003
	local IFS=$'\n'
	# shellcheck disable=SC2046
	amove $(find usr/lib/python*/site-packages/psycopg_c -type d -name __pycache__)
}

sha512sums="
1e27eec49f3ae99f1fec68816dff3974aa9b1f526df45ccca295c7c6703cc309ca4f8f65d7e3450b628906a938c65f8cbd1a1f3cb6a41b6e51dc5e220985af39  psycopg-3.1.18.tar.gz
d61e8d6a32ad43e439e906738d1046cf839a9b6576c339b342c46be3565b8863fa0a13205c98f24e8378a8b81939f8eb7465f290264ffd736736508dcb3e2331  fix-missing-type-imports.patch
f11a4be8d70117f63bc24a658f53df96be4e71f9c6eb7a515ec7a39f328ecdfc84cc299a5a4d68f61a44e0e3796ec15e54c9347e0f4be7fb3cc806f633a8c592  typing-ext.patch
630ae96920d23826db387dc65f0af3f79aff2af933e207db717061d6acfffa220e2e4e9770a663319c0e25974d5434908b7c6340d63a59ce1471dbd182a36263  psycopg-binary.patch
"
